﻿using DevExpress.Export;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPrinting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevUIUtils
{
    public static class XtraGridEx
    {
        /// <summary>
        /// GridView导出Excel
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="exportType">导出模式(DevExpress.Export.ExportType枚举类型)。</param>
        public static void ExportExcel(this GridView gridView, ExportType exportType = ExportType.WYSIWYG)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog() { Title = "导出Excel", Filter = "Excel文件(*.xlsx)|*.xlsx" };
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                //XlsExportOptionsEx optionsEx = new XlsExportOptionsEx() { ExportType = exportType, TextExportMode = TextExportMode.Text };
                //XlsExportOptions options = new XlsExportOptionsEx() { ExportType = exportType, TextExportMode = TextExportMode.Text };
                XlsxExportOptionsEx options = new XlsxExportOptionsEx() { ExportType = exportType, TextExportMode = TextExportMode.Text };
                gridView.ExportToXlsx(saveFileDialog.FileName, options);
            }
        }

        #region Styles >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        /// <summary>
        /// 设置GridView样式
        /// 1.不允许编辑，
        /// 2.不允许排序，
        /// 3.不允许过滤,
        /// 4.不允许增加和删除行
        /// 5.不允许拖移表格列
        /// 6.不允许弹出表头右键菜单
        /// 7.不允许弹出表头分组右键菜单
        /// 8.不允许显示明细展开按钮
        /// 9.不显示GroupPanel的搜索功能
        /// </summary>
        public static void LoadStyle_NoneView(this GridView gridView)
        {
            gridView.OptionsBehavior.Editable = false;
            gridView.OptionsCustomization.AllowSort = false;
            gridView.OptionsCustomization.AllowFilter = false;
            gridView.OptionsBehavior.AllowAddRows = DefaultBoolean.False;
            gridView.OptionsBehavior.AllowDeleteRows = DefaultBoolean.False;
            gridView.OptionsCustomization.AllowColumnMoving = false;
            gridView.OptionsMenu.EnableColumnMenu = false;
            gridView.OptionsMenu.EnableGroupPanelMenu = false;
            gridView.OptionsView.ShowDetailButtons = false;
            gridView.OptionsFind.AllowFindPanel = false;
        }

        /// <summary>
        /// 设置GridView样式
        /// 1.不允许排序，
        /// 2.不允许过滤,
        /// 3.不允许增加和删除行
        /// 4.不允许拖移表格列
        /// 5.不允许弹出表头右键菜单
        /// 6.不允许弹出表头分组右键菜单
        /// 7.不允许显示明细展开按钮
        /// </summary>
        public static void LoadStyle_EditView(this GridView gridView)
        {
            gridView.OptionsCustomization.AllowSort = false;
            gridView.OptionsCustomization.AllowFilter = false;
            gridView.OptionsBehavior.AllowAddRows = DefaultBoolean.False;
            gridView.OptionsBehavior.AllowDeleteRows = DefaultBoolean.False;
            gridView.OptionsCustomization.AllowColumnMoving = false;
            gridView.OptionsMenu.EnableColumnMenu = false;
            gridView.OptionsMenu.EnableGroupPanelMenu = false;
            gridView.OptionsView.ShowDetailButtons = false;
        }

        /// <summary>
        /// GridView设置序号
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="isHidFlag">是否隐藏行头上的小标签</param>
        /// <param name="iSNHeadWidth">序号宽度</param>
        public static void SetSNInRowHead(this GridView gridView, bool isHidFlag = false, int iSNHeadWidth = 35)
        {
            gridView.OptionsView.ShowIndicator = true;
            gridView.IndicatorWidth = iSNHeadWidth;
            gridView.CustomDrawRowIndicator += (send, e) =>
            {
                if (e.Info.IsRowIndicator && e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                    if (isHidFlag) e.Info.ImageIndex = -1;
                }
            };
        }

        /// <summary>
        /// 自动滚动到最后一条记录
        /// </summary>
        /// <param name="gridView"></param>
        public static void AutoScrolledToEnd(this GridView gridView)
        {
            gridView.RowCountChanged += (sender, e) =>
            {
                gridView.FocusedRowHandle = gridView.RowCount - 1;
            };
        }
        #endregion Styles <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        /// <summary>
        /// 设置右键菜单
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="menu">菜单对象</param>
        public static void SetContextMenu(this GridView gridView, PopupMenu menu)
        {
            gridView.RowClick += (sender, e) =>
            {
                if (e.Clicks == 1 && e.Button == MouseButtons.Right)
                {
                    menu.ShowPopup(Control.MousePosition);
                }
            };
        }
    }
}
